package timing.ukulele.auth.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import timing.ukulele.auth.persistent.OAuth2AuthorizationConsentSimple;
import timing.ukulele.auth.persistent.RegisteredClientSimple;

@Mapper
@Repository
public interface RegisteredClientSimpleMapper extends BaseMapper<RegisteredClientSimple> {

    /**
     * 潮汛用户所有授权的client
     *
     * @param username 用户名
     * @return
     */
    @Select("select registered_client_id as registeredClientId,principal_name as principalName,authorities from oauth2_authorization_consent where principal_name=#{username}")
    IPage<OAuth2AuthorizationConsentSimple> userConsentList(IPage<OAuth2AuthorizationConsentSimple> page, @Param("username") String username);

    /**
     * 查询某个客户端所有已授权的用户
     *
     * @param clientId 客户端id
     * @return
     */
    @Select("select registered_client_id as registeredClientId,principal_name as principalName,authorities from oauth2_authorization_consent where registered_client_id=#{clientId}")
    IPage<OAuth2AuthorizationConsentSimple> clientConsentUserList(IPage<OAuth2AuthorizationConsentSimple> page, @Param("clientId") String clientId);


    /**
     * 删除用户的授权信息
     *
     * @param username 用户名
     * @param clientId clientId
     * @return
     */
    @Delete("delete from oauth2_authorization_consent where registered_client_id=#{client} and principal_name=#{username}")
    int deleteUserConsent(@Param("username") String username, @Param("clientId") String clientId);
}
